﻿using System;
using System.Collections;
using System.Configuration;
using System.Data; 
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Bll;
using System.Text;
using Runda.CMS.Common.Utility;

namespace Runda.CMS.Portal.Finiance
{
    public partial class CustomerPerformanceView : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //if (Request.QueryString["UserID"] != null)
                //{
                    Calculate();
                    BindGrid();
                //}
            }
        }
        private void BindGrid()
        {
            string Whereclause = "";
            
            if (txtCustomerName.Text.Trim() != "")
            {
                Whereclause = " and cCusCode in ( select CustomerCode from Cms_Ctrt_Customer where CustomerName like '%" + txtCustomerName.Text.Trim() + "%' or CustomerAddress like '%" + txtCustomerName.Text.Trim() + "%')";
            }

            BillBll bll = new BillBll();
            int RecordCount = 0;
            DataTable dt = bll.GetCustomerPerformance(Whereclause, rbUfaccountno.SelectedValue.ToString(), "cCusCode", AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, out RecordCount);
            gridList.DataSource = dt;
            gridList.DataBind();
            AspNetPager1.RecordCount = RecordCount;
            hfcustsum.Value = RecordCount.ToString();
        }



        protected void gridList_ItemDataBound(object sender, Telerik.WebControls.GridItemEventArgs e)
        {
            if (e.Item.ItemType == Telerik.WebControls.GridItemType.Item || e.Item.ItemType == Telerik.WebControls.GridItemType.AlternatingItem)
            {
                LinkButton lnkbtnView = e.Item.FindControl("lnkbtnView") as LinkButton;
                DataRowView dv = e.Item.DataItem as DataRowView;
                //lnkbtnView.Attributes.Add("onclick", "openMyFullWin('CustomerBillView.aspx?CustomerCode=" + dv["cCusCode"].ToString() + "', 'viewsbilllist');");
                lnkbtnView.Attributes.Add("onclick", "openMyFullWin('/Report/ARRpt_01.aspx?CustomerCode=" + dv["cCusCode"].ToString() + "&Customername=" + dv["CustomerName"].ToString() + "', 'viewsbilllist');");
            }
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Calculate();
            BindGrid();
        }
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindGrid();
        }

        private void Calculate()
        {
            string Whereclause = "";

            if (txtCustomerName.Text.Trim() != "")
            {
                Whereclause = " and cCusCode in ( select CustomerCode from Cms_Ctrt_Customer where CustomerName like '%" + txtCustomerName.Text.Trim() + "%' or CustomerAddress like '%" + txtCustomerName.Text.Trim() + "%')";
            }
            BillBll bll = new BillBll();
            DataTable dt = bll.CalcCustomerPerformance(Whereclause, rbUfaccountno.SelectedValue.ToString());

            if (dt != null && dt.Rows.Count > 0)
            {
                //hfcustsum.Value = string.Format("{0:N}", dt.Rows[0]["custs"]);
                hfNatSUm.Value = string.Format("{0:N}", dt.Rows[0]["iNatSum"]);
                hfExchSum.Value = string.Format("{0:N}", dt.Rows[0]["iExchSum"]);
                hfAccountPayable.Value = string.Format("{0:N}", dt.Rows[0]["AccountPayable"]);
                hfAccountPayableEx.Value = string.Format("{0:N}", dt.Rows[0]["AccountPayableEx"]);
                hfReward.Value = string.Format("{0:N}", dt.Rows[0]["Reward"]);
                hfFine.Value = string.Format("{0:N}", dt.Rows[0]["Fine"]);
            }
            else
            {
                //hfcustsum.Value = "0";
                hfNatSUm.Value = "0";
                hfExchSum.Value = "0";
                hfAccountPayable.Value = "0";
                hfAccountPayableEx.Value = "0";
                hfReward.Value = "0";
                hfFine.Value = "0";
            }
        }

        protected void btnExportExcel_Click(object sender, EventArgs e)
        {
            string Whereclause = "";

            if (txtCustomerName.Text.Trim() != "")
            {
                Whereclause = " and cCusCode in ( select CustomerCode from Cms_Ctrt_Customer where CustomerName like '%" + txtCustomerName.Text.Trim() + "%' or CustomerAddress like '%" + txtCustomerName.Text.Trim() + "%')";
            }

            BillBll bll = new BillBll();

            DataTable dt = bll.ExportAllCustomerPerformance(Whereclause,rbUfaccountno.SelectedValue.ToString());

            Export.Export2Excel(dt, rbUfaccountno.SelectedItem.Text.ToString()+"客户应收账款", Page.Response);

        }
    }
}
